from scopt.util.message_types import STRING
from scopt.util import message as msg_util
from . import message


@message(type='context.tenant.param_get',
         version='0.1',
         reqd={'tenant_id': STRING(required=True, 
                                   doc='_tenant_id'),
               'param_names': [STRING(required=True, 
                                      doc='param_value')]},
         resd={'param_name': STRING(required=True, 
                                    doc='param_value')})
def handle(data_packet, context):
    reqd = msg_util.get_request_data(data_packet)
    tenant_id = reqd['tenant_id']
    param_names = reqd['param_names']
    # TODO: quering parameter of tenant, user is not not associated with,
    #       is forbidden.
    result = context.query('tenants_params',
                           columns=['tenants_params.param_name',
                                    'tenants_params.param_value'],
                           where=[['tenants_params.tenant_fk', tenant_id],
                                  ['tenants_params.scope', 'public'],
                                  ['tenants_params.param_name', param_names]],
                           mapper=_result_mapper)
    return msg_util.ok_response(data_packet, handle, result)


def _result_mapper(result):
    return {row['param_name']: row['param_value']
            for row in result}
